package com.sun.myproject.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.sun.myproject.bean.Book;
import com.sun.myproject.bean.User;
import com.sun.myproject.dao.BookDao;
import com.sun.myproject.dao.UserDao;
import com.sun.myproject.daoimpl.BookDaoImpl;
import com.sun.myproject.daoimpl.UserDaoImpl;
import com.sun.myproject.tool.ExcelUtil;

/**
 * Servlet implementation class UserImportExcelServlet
 */
@WebServlet("/UserImportExcelServlet")
@MultipartConfig
public class UserImportExcelServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserImportExcelServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 写入文件
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		System.out.println("----===-==-=-=来了");

		PrintWriter out = response.getWriter();
		// 创建目录
		File path = new File("c:\\img");
		if (!path.exists()) {
			path.mkdir();
		}
		// 获取文件
		Part img = request.getPart("excelpath");
		// 文件名

		String header = img.getHeader("content-disposition");
		System.out.println(header);
		// 获取文件名
		String fileName = getFileName(header);
		System.out.println("-------" + fileName);
		// 文件全路径
		String filePath = path.getPath() + File.separator + fileName;
		System.out.println(filePath);
		// 写入文件
		img.write(filePath);
		FileInputStream inputStream = new FileInputStream(filePath);
		ExcelUtil excelUtil = new ExcelUtil();
		try {
			List<Object> list = excelUtil.ExcelUtil(inputStream);
			String userid = "";
			String username = "";
			String passwords = "";
			String birthday = "";
			String sex = "";
			int usertype = 1;
			String isfull = "0";
			double money = 0;
			System.out.println(list.toString());

			for (int i = 0; i < list.size(); i++) {
				System.out.println(list.size());
				for (int j = 0; j <= 5; j++) {
					System.out.println(j);
					switch (j) {
					
					case 0:
						
						userid = list.remove(0).toString();
						System.out.println(userid);
						break;
					case 1:
						username = list.remove(0).toString();
						System.out.println(username);
						break;
					case 2:
						passwords = list.remove(0).toString();
						System.out.println(passwords);
						break;
					case 3:
						birthday = list.remove(0).toString();
						System.out.println(birthday);
						break;
					case 4:
						sex = list.remove(0).toString();
						System.out.println(sex);
						break;
					case 5:
						usertype =Integer.parseInt(list.remove(0).toString());
						System.out.println(usertype);
						break;

					}
				}
				
				
				//string -- date
				Date date= new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
				User User = new User(0,username, passwords,date,sex, usertype, isfull, money);
				UserDao service = new UserDaoImpl();
				service.insert(User);
				System.out.println("成功插入一条");
				//3. 跳转到列表页
				//再查一次数据库，然后再装到作用域中 ，然后再跳转。
				//这里是直接跳转到页面上， 那么这个页面会重新翻译一次，上面的那个request的请求存放的数据是没有了。 
				//request.getRequestDispatcher("list.jsp").forward(request, response);
				//servlet除了能跳jsp之外。 还能跳servlet
			

			}
			request.getRequestDispatcher("UserListServlet").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public String getFileName(String header) {

		String[] tempArr1 = header.split(";");

		String[] tempArr2 = tempArr1[2].split("=");

		String fileName = tempArr2[1].substring(tempArr2[1].lastIndexOf("\\") + 1).replaceAll("\"", "");
		return fileName;
	}

}
